import java.util.Scanner;
import java.util.Calendar;
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.io.FileReader;
import java.io.BufferedReader;

public class testclass
{
	static attribute[] array = new attribute[100];
	public static void main(String[] args) throws IOException
	{
		array[0] = new attribute();
		array[0].emptyStock();
		Scanner eingabe = new Scanner(System.in);
		int auswahl1 = 1;
		while(auswahl1 != 6)
		{
		showmenu();
		auswahl1 = eingabe.nextInt();
		clearConsole();
				switch (auswahl1)
				{
					case 1 : System.out.println("How many Shoes do you want to enter?");
							newshoe(eingabe.nextInt()); break;
					case 2 : deleteElement();break;
					case 3 : print(); break;
					case 4 : saveStock();break;
					case 5 : loadStock();break;
					case 6 : System.exit(1); break;		
					default: System.out.println("Not a valid Command!");
				}
			}	
		}
		
	
	
	
	public static void print()
	{
		int i=1;
			
		int l;
		int ZAHL = 40;
		System.out.println("Total Stock: " + array[0].getTotalstock() + "\n\n\n");
		System.out.print("|ID\t|Hersteller");
		for(l=0;l<ZAHL-10;l++)
		System.out.print(" ");
		System.out.print("|Fabrikat");
		for(l=0;l<ZAHL-8;l++)
		System.out.print(" ");
		System.out.print("|Groesse\t|Farbe");
		for(l=0;l<ZAHL-20;l++)
		System.out.print(" ");
		System.out.println("|Datum");
		System.out.println("_________________________________________________________________________________________________________________________________________");
		int j,k=0;
		if (array[0].getStockStatus())
			System.out.println("no Stock");
		else
		{
			do
			{
				if (array[i] == null)
					i++;
				else
				{
					j = array[i].getHersteller().length();
					System.out.print("|" + array[i].getId()+"\t|"+array[i].getHersteller());
					for (l=0;;l++)
					{
						if (l == ZAHL-j)
							break;
						System.out.print(" ");
					}
					j = array[i].getFabrikat().length();
					System.out.print("|" + array[i].getFabrikat());
					for (l=0;;l++)
					{
						if (l == ZAHL-j)
							break;
						
						System.out.print(" ");
					}
					j = array[i].getFarbe().length();
					System.out.print("|"+array[i].getGroesse() +"\t\t|" + array[i].getFarbe());
					for (l=0;;l++)
					{
						if (l == ZAHL-15-j)
							break;
						
						System.out.print(" ");
					}
					System.out.print("|");
					System.out.print(array[i].getYear() + "-" + array[i].getMonth() + "-" + array[i].getDay() + "\n");
					
					
					// System.out.println("|" + array[i].getId() + "\t|" + array[i].getHersteller() + " \t\t\t| " + array[i].getFabrikat() + "\t\t|" + array[i].getGroesse() + "\t\t|" + array[i].getFarbe());
					i++;
					k++;
				}
				}while(k != array[0].getTotalstock());
		}
	}
	
	public static void showmenu()
	{

		System.out.print("\n"+"\n"+"\n");
		System.out.println("++++++++++++++++++++++++ Menu +++++++++++++++++++++++++++");
		System.out.println("# 1 # - New Shoe");
		System.out.println("# 2 # - Delete Element");
		System.out.println("# 3 # - Show Stock");
		System.out.println("# 4 # - Save Stock");
		System.out.println("# 5 # - Load Stock");
		System.out.println("# 6 # - Exit");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.print("\n"+"\n"+"\n");
		
	}
	private static void clearConsole()
	{
				int i=0;
				
				for(;i<100;i++)
				{
					System.out.println("");
				}
	}
	
	public static void newshoe(int number)
	{
		int j =0;
		int i=1;
		Calendar date = Calendar.getInstance();
		
		while(array[i] != null)
		{
			i++;
		}
		for (j=i;j<number+i;j++)
		{
			Scanner eingabe = new Scanner(System.in);
			System.out.println((j-i+1) + " .Schuh: Bitte Hersteller eingeben:");
			String hersteller = eingabe.nextLine();
			System.out.println((j-i+1) + " .Schuh: Bitte Fabrikat eingeben:");
			String fabrikat = eingabe.nextLine();
			System.out.println((j-i+1) + " .Schuh: Bitte Farbe eingeben:");
			String farbe = eingabe.nextLine();
			System.out.println((j-i+1) + " .Schuh: Bitte Groesse eingeben:");
			int groesse = eingabe.nextInt();
			
			array[j] = new attribute(j, hersteller, fabrikat,groesse,farbe,date); 
		}
			
	}
	
	public static void saveStock()
	{
		int i=1;
		if (array[i] == null)
			System.out.println("no Stock: Nothing saved!");
		else
		{
		String stockfile ="stock.txt";
		PrintWriter pWriter = null; 
        try { 
            pWriter = new PrintWriter(new FileWriter(stockfile, true), true);
		int l;
		
			do
			{
				pWriter.print(array[i].getId()+","+array[i].getHersteller());
				pWriter.print("," + array[i].getFabrikat());
				pWriter.print(","+array[i].getGroesse() +"," + array[i].getFarbe());
				pWriter.print(",");
				pWriter.print(array[i].getYear() + "-" + array[i].getMonth() + "-" + array[i].getDay() + "\n");
				
				
				// pWriter.println("|" + array[i].getId() + "\t|" + array[i].getHersteller() + " \t\t\t| " + array[i].getFabrikat() + "\t\t|" + array[i].getGroesse() + "\t\t|" + array[i].getFarbe());
				i++;
				pWriter.println("");
				}while(array[i] != null);
				System.out.println("Stock wurde unter \"" + stockfile + "\"gespeichert");
			
			}catch (IOException ioe) { 
            ioe.printStackTrace(); }
		
		}
	}
	
	public static void loadStock() throws IOException
	{
		// int id,groesse,dateyear,dateday,datemonth;
		// String hersteller,fabrikat,farbe;
		FileReader reader = new FileReader("stock.txt");
		BufferedReader br = new BufferedReader(reader);
		int i=0;
		String[] tmparray;
		String teststring;
		String deli ="," ;
		while((teststring = br.readLine()) != null)
			{
				tmparray = teststring.split(deli,6);
				int l;
						int id = Integer.parseInt(tmparray[0]);
						String hersteller = tmparray[1];
						String fabrikat = tmparray[2];
						int groesse = Integer.parseInt(tmparray[3]);
						String farbe = tmparray[4];
						String date = tmparray[5];
				i++;
				array[i] = new attribute(id, hersteller, fabrikat,groesse,farbe,date);
				// System.out.println(teststring);
			}
		
		br.close();
	}
	
	public static void deleteElement()
	{
			Scanner eingabe = new Scanner (System.in);
			
			System.out.println("Welchen Eintrag willst du löschen?\n ID: ");
			int choice = eingabe.nextInt();
			
			int iddel = array[choice].getId();
			array[0].changeTotalStock();
			array[choice] = null;
			System.out.println("Du hast den Eintrag mit der ID: "+iddel+" gelöscht");
			
			if(array[0].getTotalstock() < 1)
			{
				array[0].emptyStock();
			}
			else ;
				
			
	}
	}